package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.constants.RConst;
import cn.itsource.exception.GlobalException;
import cn.itsource.pojo.domain.CustomerCar;
import cn.itsource.mapper.CustomerCarMapper;
import cn.itsource.pojo.dto.CustomerCarDto;
import cn.itsource.service.ICustomerCarService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户车辆 服务实现类
 * </p>
 *
 * @author zong
 * @since 2024-09-06
 */
@Service
public class CustomerCarServiceImpl extends ServiceImpl<CustomerCarMapper, CustomerCar> implements ICustomerCarService {

    @Override
    public Boolean save(CustomerCarDto carDto) {
        CustomerCar one = super.getOne(new LambdaQueryWrapper<CustomerCar>()
                .eq(CustomerCar::getCustomerId, StpUtil.getLoginIdAsLong())
                .eq(CustomerCar::getCarPlate, carDto.getCarPlate())
                .eq(CustomerCar::getDeleted, RConst.Num.BASE_LONG_0));
        if(one == null){
            CustomerCar customerCar = new CustomerCar();
            customerCar.setCarType(carDto.getCarType());
            customerCar.setCarPlate(carDto.getCarPlate());
            customerCar.setCreateTime(new Date());
            customerCar.setCustomerId(StpUtil.getLoginIdAsLong());
            return super.save(customerCar);
        }
        throw new GlobalException("该:"+carDto.getCarPlate()+"车牌已添加过了");
    }

    @Override
    public List<CustomerCar> listCar() {
        return super.list(new LambdaQueryWrapper<CustomerCar>()
                .eq(CustomerCar::getCustomerId, StpUtil.getLoginIdAsLong())
                .eq(CustomerCar::getDeleted, RConst.Num.BASE_LONG_0));
    }
}
